package CutLoad.transitions
{
	/**
	 * 弹性运动
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	public class SpringMove extends TBase
	{
		//缓动参数
		protected var offset:Number;
		//目标坐标
		protected var startNum:Number,targetNum:Number;
		protected var endSet:Number;
		//速度
		protected var vv:Number;
		//摩擦力
		protected var friction:Number;
		//
		protected var record:Record;
		/**
		 * 初始化
		 * @param startNum 开始值
		 * @param targetNum 目标值
		 * @param offset 参数0~1
		 * @param friction 摩擦力 
		 * @param endSet 结束参数0~1
		 * 
		 */		
		public function SpringMove(startNum:Number,targetNum:Number,offset:Number=0.1,friction:Number=0.95,endSet:Number=0.5)
		{
			vv=0;
			this.stop();
			this.startNum=startNum;
			this.currNum=startNum;
			this.targetNum=targetNum;
			this.offset=offset;
			this.friction=friction;
			this.endSet=endSet;
			record=new Record();
			super.initAllFun();
		}
		override public function start(someData:TransitionsData=null):void{
			super.start();
			this.currNum=startNum;
		}
		/**
		 * 获得这样的弹簧效果：弹出一次然后回到中点 .注：类似弹出窗口的效果
		 * @param startNum  开始值
		 * @param targetNum 目标值
		 * @return 
		 * 
		 */		
		public static function getSpringBakEnd(startNum:Number,targetNum:Number):SpringMove{
			var tem:SpringMove=new SpringMove(startNum,targetNum,0.2,0.68);
			return tem
		}
		override public function enterFrame():void
		{
			if(cs==0) return;
			
			var dNum:Number=targetNum-this.currNum;
			if(Math.abs(dNum)<endSet){
				this.currNum=this.targetNum;
				this.stop();
				this.funEnd(this.currNum);
				record.push(currNum);
				return;
			}
			var ax:Number=dNum*this.offset;
			vv+=ax;
			vv*=this.friction;
			this.currNum+=vv;
			funEnterFrame(this.currNum);
			record.push(currNum);
		}
		
		/**
		 * 设置 循环时要调用的外部函数
		 * @param fun 格式:function(当前值:Number):void
		 * 
		 */		
		override public function setFunEnterFrame(fun:Function):void{
			this.funEnterFrame=fun;
		}
		/**
		 * 设置结束时要调用的外部函数 
		 * @param fun 格式:function(当前值:Number):void
		 * 
		 */
		override public function setFunEnd(fun:Function):void{
			this.funEnd=fun;
		}
		/**
		 * 返回效果在此过程中的数据记录对象 
		 * @return 
		 * 
		 */		
		public function getRecord():Record{
			return record
		}
	}
}